using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using TMS.Application.Common.Interface;
using TMS.Application.RequestDto;
using TMS.Domain.Entity;

namespace TMS.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class AddCourseController : ControllerBase
    {
        private readonly IRepository<Students> _Students;
        private readonly IRepository<College> _College;
        private readonly IRepository<Major> _Major;
        private readonly IRepository<Avatar> _Avatar;
        private readonly IRepository<Class> _Class;
        private readonly IRepository<StuUsers> _StuUsers;
        private readonly IRepository<Course> _Course;
        private readonly IRepository<Term> _Term;

        public AddCourseController(IRepository<Term> Term, IRepository<Course> Course, IRepository<StuUsers> StuUsers, IRepository<Avatar> Avatar, IRepository<Class> Class, IRepository<Major> Major, IRepository<College> College, IRepository<Students> Students)
        {
            _Term = Term;

            _Course = Course;

            _StuUsers = StuUsers;

            _Avatar = Avatar;

            _Class = Class;

            _Major = Major;

            _College = College;

            _Students = Students;
        }

        [HttpPost("/GetCourse")]
        public async Task<object> CourseData(CourseDto courseDto)
        {

            var course = _Course.Table.FirstOrDefault(x => x.ClassId == courseDto.ClassId && x.TermId == courseDto.TermId);



            if (course == null || course.CourseText == null)
            {

                var terms = _Term.Table.ToList();
                var stuClass = _Class.Table.ToList();
                var className = stuClass.FirstOrDefault(x => x.Id == courseDto.ClassId);

                if (className != null)
                {
                    var text = "{'0':{'0':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'1':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'2':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'3':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'4':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'5':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'6':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'}},'1':{'0':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'1':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'2':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'3':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'4':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'5':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'6':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'}},'2':{'0':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'1':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'2':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'3':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'4':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'5':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'6':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'}},'3':{'0':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'1':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'2':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'3':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'4':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'5':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'6':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'}},'4':{'0':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'1':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'2':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'3':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'4':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'5':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'6':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'}},'5':{'0':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'1':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'2':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'3':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'4':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'5':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'6':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'}},'6':{'0':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'1':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'2':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'3':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'4':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'5':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'6':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'}},'7':{'0':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'1':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'2':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'3':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'4':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'5':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'6':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'}},'8':{'0':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'1':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'2':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'3':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'4':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'5':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'6':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'}},'9':{'0':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'1':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'2':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'3':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'4':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'5':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'6':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'}}}";

                    List<Course> list = new List<Course>();

                    foreach (var item in terms)
                    {
                        var data = new Course
                        {

                            ClassId = className.Id,

                            TermId = item.Id,

                            CourseText = text

                        };

                        list.Add(data);
                    }
                    try
                    {
                        await _Course.AddBulkAsync(list);

                        var course2 = _Course.Table.FirstOrDefault(x => x.ClassId == courseDto.ClassId && x.TermId == courseDto.TermId);

                        return new
                        {

                            code = "400",
                            mag = "无效的班级信息",
                            data = course2.CourseText

                        };


                    }
                    catch (Exception ex)
                    {
                        return new
                        {

                            code = "400",
                            mag = "失败",
                            data = ex

                        };

                    }



                }
                else
                {
                    return new
                    {

                        code = "400",
                        mag = "无效的班级信息",
                        data = ""

                    };


                }

            }

            if (course.CourseText == "")
            {

                course.CourseText = "{'0':{'0':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'1':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'2':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'3':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'4':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'5':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'6':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'}},'1':{'0':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'1':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'2':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'3':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'4':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'5':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'6':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'}},'2':{'0':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'1':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'2':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'3':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'4':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'5':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'6':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'}},'3':{'0':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'1':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'2':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'3':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'4':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'5':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'6':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'}},'4':{'0':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'1':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'2':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'3':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'4':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'5':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'6':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'}},'5':{'0':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'1':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'2':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'3':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'4':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'5':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'6':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'}},'6':{'0':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'1':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'2':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'3':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'4':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'5':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'6':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'}},'7':{'0':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'1':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'2':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'3':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'4':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'5':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'6':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'}},'8':{'0':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'1':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'2':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'3':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'4':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'5':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'6':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'}},'9':{'0':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'1':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'2':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'3':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'4':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'5':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'6':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'}}}";

                await _Course.UpdateAsync(course);

                course =  _Course.Table.FirstOrDefault(x => x.ClassId == courseDto.ClassId && x.TermId == courseDto.TermId);
            }

            return course.CourseText;


        }
        [HttpPost("/GetAddCourse")]
        public async Task<object> AddCourse(Course course)
        {
            try
            {
                var courses = _Course.Table.FirstOrDefault(x => x.ClassId == course.ClassId && x.TermId == course.TermId);

                courses.CourseText = course.CourseText;

                await _Course.UpdateAsync(courses);

                return new
                {

                    code = "200",
                    mag = "操作成功",
                    data = courses.CourseText

                };
            }
            catch (System.Exception)
            {

                return new
                {

                    code = "200",
                    mag = "操作失败"

                };
            }




        }
    }
}